package com.sgb.goods.view.activity;

import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityGoodsAlbumBinding;
import com.sgb.goods.entity.AlbumEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.view.adapter.publish.SelectNetImgAdapter;
import com.sgb.goods.viewmodel.publish.GoodsAlbumModel;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;
@Route(path = Constants.ARouter.AR_GOODS_ALBUM)
public class GoodsAlbumActivity extends BaseActivity<GoodsActivityGoodsAlbumBinding, GoodsAlbumModel> {

    private String id;
    private String name;
//    private GoodsEvent goodsEvent;
    private List<AlbumEntity> datas;
    private List<AlbumEntity> albumListName;
    private SelectNetImgAdapter adapter;
    private List<String> selectUrl;
    private int albumPosition;
    private int size;

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_goods_album;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(GoodsAlbumModel.class, BR.viewModel);
    }

    @Override
    protected void init() {
        id = getIntent().getStringExtra("id");
        name = getIntent().getStringExtra("name");
        albumPosition = getIntent().getIntExtra("position", 0);
        albumListName = (List<AlbumEntity>) getIntent().getSerializableExtra("datas");
        mDataBinding.tvTitle.setText(name);

        mDataBinding.rvAlbum.setLayoutManager(new GridLayoutManager(GoodsAlbumActivity.this, 3));
        if (adapter == null)
            adapter = new SelectNetImgAdapter();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                if (ada.size() != 0) {
//                    mDataBinding.tvCancel.setVisibility(View.VISIBLE);
//                    mDataBinding.backView.setVisibility(View.GONE);
//                } else {
//                    mDataBinding.tvCancel.setVisibility(View.GONE);
//                    mDataBinding.backView.setVisibility(View.VISIBLE);
//                }
                //mDataBinding.tvCompany.setText("完成(" + albumEntities.size() + ")");
            }
        });
        mDataBinding.rvAlbum.setAdapter(adapter);
        mDataBinding.rvAlbum.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mViewModel.getSupplierPhotosByAlbumId(false, id);
            }

            @Override
            public void onLoadMore() {
            }
        });
        mDataBinding.rvAlbum.setLoadingMoreEnabled(false);

        mViewModel.getSupplierPhotosByAlbumId(false, id);
    }

    @Override
    protected void initObserve() {
        mViewModel.albumListLD.observe(this, list -> {

        });
    }

    public void showList(List<AlbumEntity> data, boolean isLodMore) {
        this.datas = data;
        adapter.setNewData(data);
        mDataBinding.rvAlbum.refreshComplete();
        if (selectUrl != null && selectUrl.size() != 0) {
            //取消默认的选中，保持和IOS一致
            for (int i = 0; i < selectUrl.size(); i++) {
                for (int j = 0; j < data.size(); j++) {
                    if (selectUrl.get(i).equals(data.get(j).imageUrl)) {
                        //adapter.setCheckDefault(j);
                        size++;
                    }
                }
            }
            //adapter.setMost(size);
            mDataBinding.tvCancel.setVisibility(View.VISIBLE);
            mDataBinding.backView.setVisibility(View.GONE);
        } else {
            //adapter.setMost(8);
            mDataBinding.tvCancel.setVisibility(View.GONE);
            mDataBinding.backView.setVisibility(View.VISIBLE);
        }
        //mDataBinding.tvCompany.setText("完成(" + adapter.getCheckDatas().size() + ")");
    }

    public void showAlbumPop() {
        //条件选择器
        OptionsPickerView optionsPickerView = new OptionsPickerBuilder(GoodsAlbumActivity.this, (options1, options2, options3, v) -> {
            String content = albumListName.get(options1).name;
            mDataBinding.tvTitle.setText(content);
            albumPosition = options1;
            id = albumListName.get(albumPosition).id;
            //adapter.resetCheckDatas();
            mDataBinding.tvCancel.setVisibility(View.GONE);
            mDataBinding.backView.setVisibility(View.VISIBLE);
            mViewModel.getSupplierPhotosByAlbumId(false, id);
        }).build();
        optionsPickerView.setPicker(albumListName);
        optionsPickerView.setSelectOptions(albumPosition);
        optionsPickerView.setTitleText("图库选择");
        optionsPickerView.show();
    }

    @Override
    protected void initListener() {
        mDataBinding.backView.setOnClickListener(v -> finish());
        mDataBinding.tvCancel.setOnClickListener(v -> {
            //adapter.resetCheckDatas();
            mDataBinding.tvCompany.setText("完成(0)");
            mDataBinding.tvCancel.setVisibility(View.GONE);
            mDataBinding.backView.setVisibility(View.VISIBLE);
        });
        mDataBinding.replaceAlbum.setOnClickListener(v -> showAlbumPop());
        mDataBinding.tvCompany.setOnClickListener(v -> {
//            List<AlbumEntity> select = adapter.getCheckDatas();
//            if (selectUrl == null)
//                selectUrl = new ArrayList<>();
//            selectUrl.clear();
//            for (int i = 0; i < select.size(); i++) {
//                selectUrl.add(select.get(i).getImageUrl());
//            }
//            List<AlbumEntity> allData = adapter.getDatas();
//            allData.removeAll(select);
//            List<String>  unSelectUrl =new ArrayList<>();
//            for (int a = 0; a < allData.size(); a++) {
//                unSelectUrl.add(allData.get(a).getImageUrl());
//            }
        });
    }

//    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
//    public void onEvent(MaterialEvent event) {
//        if (event.getMessageTag() == EventConstant.EVENT_GOODS_NET_IMG_SELECT_RE) {
//            size = (int) event.getObject();
//            selectUrl = new ArrayList<>();
//            selectUrl = (List<String>) event.getMessageData();
//        }
//        EventBus.getDefault().removeStickyEvent(event);
//    }
}

